package yaner.hotel.manager.api.service.impl;

import lombok.extern.slf4j.Slf4j;
import org.apache.rocketmq.client.producer.SendCallback;
import org.apache.rocketmq.client.producer.SendResult;
import org.apache.rocketmq.spring.core.RocketMQTemplate;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import yaner.hotel.manager.api.dto.OrderDto;
import yaner.hotel.manager.api.service.OrderService;
import yaner.hotel.manager.api.utils.SnowUtils;

import javax.annotation.Resource;

@Slf4j
@Service
public class OrderServiceImpl implements OrderService {

    @Resource
    private RocketMQTemplate rocketMQTemplate;
    @Value("${rocketmq.topics.order}")
    private String orderTopic;

    @Override
    public boolean placeOrder(OrderDto orderDto) {
        orderDto.setOrderId(SnowUtils.nextId());
        rocketMQTemplate.convertAndSend(orderTopic + ":", orderDto);
        return true;
    }

    @Override
    public boolean syncPlaceOrder(OrderDto orderDto) {
        rocketMQTemplate.asyncSend(orderTopic, orderDto, new SendCallback() {
            // 实现消息发送成功的后续处理
            public void onSuccess(SendResult var1) {
                log.info("async onSuccess SendResult: {}", var1);
            }
            // 实现消息发送失败的后续处理
            public void onException(Throwable var1) {
                log.error("async onException Throwable: {}", var1.getMessage());
            }
        });
        return true;
    }
}
